package com.three.lab.page.common;


import android.animation.ArgbEvaluator;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.three.lab.MainActivity;
import com.three.lab.R;
import com.three.lab.base.AppManager;
import com.three.lab.base.BaseActivity;
import com.three.lab.bean.BaseBean;
import com.three.lab.widget.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends BaseActivity {

    private RelativeLayout mRootLayout;
    private ViewPager viewPager; //分页显示器
    private Button btnStart; //开始体验按钮
    private ViewPagerIndicator mIndicator;
    private int colorBg[];
    private ArgbEvaluator mArgbEvaluator;//渐变色效果类
    private Animation animationIn,animationOut;


    @Override
    protected void initParms(Bundle bundle) {
        setTransparent(true);
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        mRootLayout = $(R.id.rl_root);
        viewPager = $(R.id.viewpager_guide);
        btnStart = $(R.id.btn_guide_start);
        mIndicator = $(R.id.indicator_guide);
        mArgbEvaluator = new ArgbEvaluator();
        colorBg = getResources().getIntArray(R.array.splash_bg);
    }

    @Override
    protected void setListener() {
        $(R.id.btn_guide_start).setOnClickListener(this);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //给布局设置初始颜色
                mRootLayout.setBackgroundColor(colorBg[position]);
                //计算不同页面的结束颜色，最后一张的颜色是第一个颜色，其他的分别加一
                int endColor = position == colorBg.length - 1 ? colorBg[0]:colorBg[position + 1];
                //根据positionOffset得到渐变色，因为positionOffset本身为0~1之间的小数所以无需多做处理了
                int evaluate = (int)mArgbEvaluator.evaluate(positionOffset,colorBg[position],endColor);
                mRootLayout.setBackgroundColor(evaluate);
            }

            @Override
            public void onPageSelected(int position) {
                if(position == (colorBg.length - 1)){
                    btnStart.setVisibility(View.VISIBLE);
                    btnStart.startAnimation(animationIn);
                } else {
                    btnStart.setVisibility(View.GONE);
                    if (btnStart.getVisibility() == View.VISIBLE) {
                        btnStart.startAnimation(animationOut);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });

    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()){
            case R.id.btn_guide_start:
                AppManager.setHasEnter(true);
                finish();
                startActivity(MainActivity.class);
                break;
        }
    }

    @Override
    protected void initData(Context mContext) {
        animationIn = AnimationUtils.loadAnimation(this,R.anim.anim_bottom_in);
        animationOut = AnimationUtils.loadAnimation(this,R.anim.anim_bottom_out);

        ArrayList dataList = new ArrayList<>();
        BaseBean bb0 = new BaseBean();
        bb0.setId(R.mipmap.splash_iv_first);
        bb0.setText(getString(R.string.first_use));
        dataList.add(bb0);

        BaseBean bb1 = new BaseBean();
        bb1.setId(R.mipmap.splash_iv_second);
        bb1.setText(getString(R.string.okay));
        dataList.add(bb1);

        BaseBean bb2 = new BaseBean();
        bb2.setId(R.mipmap.splash_iv_third);
        bb2.setText(getString(R.string.give_point));
        dataList.add(bb2);

        BaseBean bb3 = new BaseBean();
        bb3.setId(R.mipmap.splash_iv_forth);
        bb3.setText(getString(R.string.share));
        dataList.add(bb3);
        final IntroPager introPager = new IntroPager(dataList);
        viewPager.setAdapter(introPager);
        viewPager.setOffscreenPageLimit(dataList.size()-1);
        mIndicator.setViewPager(viewPager);

    }

    private class IntroPager extends PagerAdapter {

        private List<BaseBean> mList;

        public IntroPager(List<BaseBean> list) {
            this.mList = list;
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemLayout = getLayoutInflater().inflate(R.layout.item_guide, container, false);
            ImageView mImage = itemLayout.findViewById(R.id.iv_img);
            TextView mTextView = itemLayout.findViewById(R.id.tv_desc);
            BaseBean baseBean = mList.get(position);
            mImage.setImageResource(baseBean.getId());
            mTextView.setText(baseBean.getText());
            container.addView(itemLayout);
            return itemLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }
    }

}